const Promise = require('bluebird');

module.exports = function ({PendingGoodses, InvalidGoodses, Goodses, CaptureLogs, sequelize}) {

    /**
     * 查询商品
     */
    Goodses.browseV2 = function (query, order) {
        let sql = 'select g.id, g.type, g.userType, g.shopTitle, g.pictUrl, g.title, g.auctionId, g.auctionUrl, g.reservePrice, g.zkPrice, g.quanPrice, g.activityId, g.couponAmount, g.sellerId, g.dsr, g.provcity, g.commission, g.commissionType, g.biz30day, g.isNew, g.isHot, g.isJhs, g.isTqg, g.isPP, g.isJYJ, g.isAli, g.isYFX, g.isJP, g.isHT, g.feature, g.order_by from Goodses g where 1 = 1 ';
        if (query.ids && query.ids.length) {
            sql += ' and g.id in (:ids)';
        }
        if (query.goods_ids && query.goods_ids.length) {
            sql += ' and g.auctionId in (:goods_ids)';
        }
        if (query.leafCatId && query.leafCatId.length) {
            sql += ' and g.leafCatId in (:leafCatId)';
        }
        if (query.sellerId) {
            sql += ' and g.sellerId = :sellerId';
        }
        if (query.auctionId) {
            sql += ' and g.auctionId != :auctionId';
        }
        if (query.price_start) {
            sql += ' and g.quanPrice >= :price_start';
        }
        if (query.price_end) {
            sql += ' and g.quanPrice <= :price_end';
        }
        if (query.keyword) {
            query.keyword = '%' + query.keyword + '%';
            sql += ' and g.title like :keyword';
        }

        if (query.isNew) {
            sql += ' and g.isNew = 1';
        }
        if (query.isHot) {
            sql += ' and g.isHot = 1';
        }
        if (query.isJhs) {
            sql += ' and g.isJhs = 1';
        }
        if (query.isTqg) {
            sql += ' and g.isTqg = 1';
        }
        if (query.isJYJ) {
            sql += ' and g.isJYJ = 1';
        }
        if (query.isPP) {
            sql += ' and g.isPP = 1';
        }

        if (query.isYFX) {
            sql += ' and g.isYFX = 1';
        }
        if (query.isJP) {
            sql += ' and g.isJP = 1';
        }
        if (query.isHT) {
            sql += ' and g.isHT: = 1';
        }

        if (query.is99) {
            sql += ' and g.is99 = 1';
        }
        if (query.is30) {
            sql += ' and g.is30 = 1';
        }
        if (query.is199) {
            sql += ' and g.is199 = 1';
        }

        if (order) {
            sql += ' order by ' + order;
        }
        let offset = query.offset ? parseInt(query.offset) : 0;
        let limit = query.limit ? parseInt(query.limit) : 20;
        sql += ' limit ' + offset + ',' + limit;

        return sequelize.query(sql, {
            type: sequelize.QueryTypes.SELECT,
            replacements: query
        });
    }

    /**
     * 降低分数值
     */
    Goodses.updateOrderBy = function () {
        return sequelize.query('update Goodses set order_by = FLOOR(order_by/2)');
    }

}